import {defineStore} from "pinia";
import {ref, watch} from "vue";
import {hasPermission} from "@/utils/permission.ts";
import {
    getOrderDelivery,
    getOrderList,
    getOrderStatus,
    orderCreate,
    orderDelete, orderDeliveryCreate, orderDeliveryUpdate,
    orderStatusCreate, orderStatusDelete, orderStatusUpdate,
    orderUpdate
} from "@/services/api/order.ts";
import useMessage from "@/utils/useMessage.ts";
import {useOrgStore} from "@/store/modules/organization";
import {Modal} from "ant-design-vue";

export const useOrderStore = defineStore('order', ()=>{
    // 订单列表
    const orderList = ref<any>([])
    // 订单状态
    const orderStatusList = ref<StatusType[]>([])
    // 订单配送方式
    const orderDeliveryList = ref<DictionaryType[]>([])
    // 加载状态
    const loading = ref<boolean>(false)

    const orgStore = useOrgStore()

    const getOrders = async (force?:boolean)=>{
        if(!force && orderList.value.length > 0){
             return
        }
        try {
            loading.value = true
            const res = await getOrderList()
            if (res.code === 200){
                orderList.value = res.data
            }
        }catch{
        }finally {
            setTimeout(()=>loading.value = false,100)
        }
    }
    const create = async (data:object)=>{
        if(!hasPermission('order:create')) return false
        const res = await orderCreate(data)
        if (res.code === 200){
            await getOrders(true)
        }
        useMessage(res.code,res.message)
        return res.code === 200
    }
    const update = async (id:number,data:object)=>{
        if(!hasPermission('order:update')) return false
        const res = await orderUpdate(id,data)
        if (res.code === 200){
            //无请求刷新更新订单列表
            const item = orderList.value.find((item:any)=>item.id === id)
            if (item){
                Object.assign(item,data)
            }
        }
        useMessage(res.code,res.message)
        return res.code === 200
    }
    const remove = async (id:number)=>{
        if(!hasPermission('order:delete')) return false
        const res = await orderDelete(id)
        if (res.code === 200){
            orderList.value = orderList.value.filter((item:any)=>item.id !== id)
        }
        useMessage(res.code,res.message)
        return res.code === 200
    }

    const getStatus = async (force?:boolean)=>{
        if(!force && orderStatusList.value.length > 0){
             return
        }
        try {
            loading.value = true
            const res = await getOrderStatus()
            if (res.code === 200){
                orderStatusList.value = res.data
            }
        }catch{
        }finally {
            setTimeout(()=>loading.value = false,100)
        }
    }
    const statusCreate = async (data:StatusFormType)=>{
        if(!hasPermission('admin:order:create')) return false
        const res = await orderStatusCreate(data)
        if (res.code === 200){
            await getStatus(true)
        }
        useMessage(res.code,res.message)
        return res.code === 200
    }
    const statusUpdate = async (id:number,data:StatusFormType)=>{
        if(!hasPermission('admin:order:update')) return false
        const res = await orderStatusUpdate(id, data)
        if (res.code === 200){
            const item = orderStatusList.value.find((item:StatusType)=>item.id === id)
            if (item){
                Object.assign(item,data)
            }
        }
        useMessage(res.code,res.message)
        return res.code === 200
    }
    const statusDelete = async (id:number)=>{
        if(!hasPermission('admin:order:delete')) return false
        Modal.confirm({
            title: '删除订单状态',
            content: '确定要删除此订单状态吗？',
            onOk: async ()=>{
                const res = await orderStatusDelete(id)
                if (res.code === 200){
                    orderStatusList.value = orderStatusList.value.filter(item=>item.id !== id)
                }
                useMessage(res.code,res.message)
                return res.code === 200
            }
        })
    }

    const getDelivery = async (force?:boolean)=>{
        if(!force && orderDeliveryList.value.length > 0){
             return
        }
        try {
            loading.value = true
            const res = await getOrderDelivery()
            if (res.code === 200){
                orderDeliveryList.value = res.data
            }
        }catch{
        }finally {
            setTimeout(()=>loading.value = false,100)
        }
    }
    const deliveryCreate = async (data:DictionaryFormType)=>{
        if(!hasPermission('admin:order:create')) return false
        const res  = await orderDeliveryCreate(data)
        if (res.code === 200){
            await getDelivery(true)
        }
        useMessage(res.code,res.message)
        return res.code === 200
    }
    const deliveryUpdate = async (id:number,data:DictionaryFormType)=>{
        if(!hasPermission('admin:order:update')) return false
        const res = await orderDeliveryUpdate(id,data)
        if (res.code === 200){
            const items = orderDeliveryList.value.find(item=>item.id === id)
            if (items){
                Object.assign(items,data)
            }
        }
        useMessage(res.code,res.message)
        return res.code === 200
    }
    const deliveryDelete = async (id:number)=>{
        if(!hasPermission('admin:order:delete')) return false
        Modal.confirm({
            title: '删除配送方式',
            content: '确定要删除此配送方式吗？',
            onOk: async ()=>{
                const res = await orderStatusDelete(id)
                if (res.code === 200){
                    orderDeliveryList.value = orderDeliveryList.value.filter(item=>item.id !== id)
                }
                useMessage(res.code,res.message)
                return res.code === 200
            }
        })
    }
    // 监听组织id变化，刷新订单配置信息
    watch(()=>orgStore.id,async ()=>{
        await getOrders(true)
        await getStatus(true)
        await getDelivery(true)
    })
    return {
        loading,
        orderList,
        orderStatusList,
        orderDeliveryList,
        getOrders,
        create,
        update,
        remove,
        getStatus,
        statusCreate,
        statusUpdate,
        statusDelete,
        getDelivery,
        deliveryCreate,
        deliveryUpdate,
        deliveryDelete
    }
},{persist:{omit:['loading']}})